package cn.wwcyc.controller;

import cn.wwcyc.domain.*;
import cn.wwcyc.mapper.TjJygywYzMapper;
import cn.wwcyc.service.TjDwService;
import cn.wwcyc.service.TjJygywYzService;
import cn.wwcyc.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/jygywyz")
public class JygywYzController {
    @Autowired
    private TjJygywYzService tjJygywYzService;
    @Autowired
    private TjJygywYzMapper jygywYzMapper;
    @Autowired
    UserService userService;
    @Autowired
    TjDwService tjDwService;
    @RequestMapping("/getBydwJh")
    public R<List<TjJygywYzVo>> getBydwJh(
            @RequestParam( required = false) Long dwid,
            @RequestParam( required = false) String jh,
            HttpServletRequest request
    ){
        //TODO:
        User user = userService.getCurrentUser(request);
        if(dwid == null)
            dwid = user.getDw();

        TjDw tjDw = null;
        if (dwid != null) {
            tjDw = tjDwService.getDwTreeById(dwid);
        }
        List<TjDw> dwlist = new ArrayList<>();
        dwlist = tjDwService.DwTreeToList(dwlist, tjDw);
        List<Long> dwIdList = new ArrayList<>();
        for (TjDw tjDw1 : dwlist) {
            dwIdList.add(tjDw1.getId());
        }
        List<TjJygywYzVo> list = jygywYzMapper.getBydwJh(dwIdList, jh);
        return R.success(list);
    }

    @RequestMapping("/edit")
    public R edit(@RequestBody TjJygywYz jygywYz, HttpServletRequest request){
        User user = userService.getCurrentUser(request);
        jygywYz.setSzr(user.getId());
        jygywYz.setSzsj(new Date());
        tjJygywYzService.updateById(jygywYz);
        return R.success();
    }
}
